from typing import List

from py7zr import DecompressionError

from interface.unzip_interfaces import DecompressionTool


class WinRarDecompressor(DecompressionTool):

    def build_command(self) -> List[str]:
        """构建WinRAR解压命令

        Returns:
            List[str]: 构建好的命令列表

        Raises:
            DecompressionError: 当缺少必要参数时抛出
        """
        if not self._config.input_path:
            raise DecompressionError("未指定输入文件")
        if not self._config.output_path:
            raise DecompressionError("未指定输出目录")

        cmd = ["winrar", "x", "-y"]
        if self._config.password:
            cmd.extend(["-p" + self._config.password])

        cmd.extend([
            str(self._config.input_path),
            "-op" + str(self._config.output_path)
        ])

        self.log.info(f"构建命令：{' '.join(cmd)}")
        return cmd


if __name__ == '__main__':
    decompressor = WinRarDecompressor()
    decompressor.set_input_path(r"E:\myproject\python\extract-script\log\unzip.7z").set_password(
        "123456").set_output_path("D:\\test.txt")
    decompressor.build_command()
